package com.zp.business.impl.patVisitIndex;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.entity.hospitalizationNotice.HospitalizationNotice;
import com.zp.business.entity.patVisit.vo.PatVisitVo;
import com.zp.business.entity.patVisitIndex.PatVisitIndex;
import com.zp.business.entity.patVisitIndex.bo.PatVisitIndexBo;
import com.zp.business.entity.patVisitIndex.vo.PatVisitIndexVo;
import com.zp.business.mapper.hospitalizationNotice.HospitalizationNoticeMapper;
import com.zp.business.mapper.patVisitIndex.PatVisitIndexMapper;
import com.zp.business.service.patVisit.IPatVisitService;
import com.zp.business.service.patVisitIndex.IPatVisitIndexService;
import com.zp.common.core.constant.BaseConstants;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 患者入院基本信息Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-06-24
 */
@RequiredArgsConstructor
@Service
public class PatVisitIndexServiceImpl implements IPatVisitIndexService {

    private final PatVisitIndexMapper baseMapper;

    private final IPatVisitService patVisitService;

    private final HospitalizationNoticeMapper hospitalizationNoticeMapper;
    /**
     * 查询患者入院基本信息
     */
    @Override
    public PatVisitIndexVo queryById(String visitIndexId) {
        return baseMapper.selectVoById(visitIndexId);
    }

    /**
     * 查询患者入院基本信息列表(分页)
     */
    @Override
    public TableDataInfo<PatVisitIndexVo> queryPageList(PatVisitIndexBo bo, PageQuery pageQuery) {

        LambdaQueryWrapper<PatVisitIndex> lqw = buildQueryWrapper(bo);
        Page<PatVisitIndexVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询患者入院基本信息列表(不分页)
     */
    @Override
    public List<PatVisitIndexVo> queryList(PatVisitIndexBo bo) {
        LambdaQueryWrapper<PatVisitIndex> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PatVisitIndex> buildQueryWrapper(PatVisitIndexBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PatVisitIndex> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getVisitId()), PatVisitIndex::getVisitId, bo.getVisitId());
        lqw.eq(StringUtils.isNotBlank(bo.getNewbornFlag()), PatVisitIndex::getNewbornFlag, bo.getNewbornFlag());
        lqw.like(StringUtils.isNotBlank(bo.getName()), PatVisitIndex::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getInputCode()), PatVisitIndex::getInputCode, bo.getInputCode());
        lqw.eq(StringUtils.isNotBlank(bo.getHeight()), PatVisitIndex::getHeight, bo.getHeight());
        lqw.eq(bo.getWeight() != null, PatVisitIndex::getWeight, bo.getWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), PatVisitIndex::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getMarriage()), PatVisitIndex::getMarriage, bo.getMarriage());
        lqw.eq(StringUtils.isNotBlank(bo.getOccupation()), PatVisitIndex::getOccupation, bo.getOccupation());
        lqw.eq(bo.getAge() != null, PatVisitIndex::getAge, bo.getAge());
        lqw.eq(bo.getAgeMonth() != null, PatVisitIndex::getAgeMonth, bo.getAgeMonth());
        lqw.eq(bo.getAgeDay() != null, PatVisitIndex::getAgeDay, bo.getAgeDay());
        lqw.eq(bo.getAgeHours() != null, PatVisitIndex::getAgeHours, bo.getAgeHours());
        lqw.eq(bo.getAgeMinutes() != null, PatVisitIndex::getAgeMinutes, bo.getAgeMinutes());
        lqw.eq(StringUtils.isNotBlank(bo.getNation()), PatVisitIndex::getNation, bo.getNation());
        lqw.eq(StringUtils.isNotBlank(bo.getNationality()), PatVisitIndex::getNationality, bo.getNationality());
        lqw.eq(StringUtils.isNotBlank(bo.getCanBaoType()), PatVisitIndex::getCanBaoType, bo.getCanBaoType());
        lqw.eq(bo.getBirthdayDate() != null, PatVisitIndex::getBirthdayDate, bo.getBirthdayDate());
        lqw.eq(StringUtils.isNotBlank(bo.getNativePlace()), PatVisitIndex::getNativePlace, bo.getNativePlace());
        lqw.eq(StringUtils.isNotBlank(bo.getNativePlaceCode()), PatVisitIndex::getNativePlaceCode, bo.getNativePlaceCode());
        lqw.eq(StringUtils.isNotBlank(bo.getZipCode()), PatVisitIndex::getZipCode, bo.getZipCode());
        lqw.eq(StringUtils.isNotBlank(bo.getHomePlaceCode()), PatVisitIndex::getHomePlaceCode, bo.getHomePlaceCode());
        lqw.eq(StringUtils.isNotBlank(bo.getHomePlace()), PatVisitIndex::getHomePlace, bo.getHomePlace());
        lqw.eq(StringUtils.isNotBlank(bo.getAddressCode()), PatVisitIndex::getAddressCode, bo.getAddressCode());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), PatVisitIndex::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getIdentity()), PatVisitIndex::getIdentity, bo.getIdentity());
        lqw.eq(StringUtils.isNotBlank(bo.getIdNo()), PatVisitIndex::getIdNo, bo.getIdNo());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), PatVisitIndex::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getContacts()), PatVisitIndex::getContacts, bo.getContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getContactsAddress()), PatVisitIndex::getContactsAddress, bo.getContactsAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getContactsAddressCode()), PatVisitIndex::getContactsAddressCode, bo.getContactsAddressCode());
        lqw.eq(StringUtils.isNotBlank(bo.getContactsPhone()), PatVisitIndex::getContactsPhone, bo.getContactsPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getContactsRelation()), PatVisitIndex::getContactsRelation, bo.getContactsRelation());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitInContractId()), PatVisitIndex::getUnitInContractId, bo.getUnitInContractId());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitInContract()), PatVisitIndex::getUnitInContract, bo.getUnitInContract());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkContract()), PatVisitIndex::getWorkContract, bo.getWorkContract());
        lqw.eq(StringUtils.isNotBlank(bo.getAffiliatingArea()), PatVisitIndex::getAffiliatingArea, bo.getAffiliatingArea());
        lqw.eq(StringUtils.isNotBlank(bo.getInsuranceType()), PatVisitIndex::getInsuranceType, bo.getInsuranceType());
        lqw.eq(StringUtils.isNotBlank(bo.getIsCard()), PatVisitIndex::getIsCard, bo.getIsCard());
        lqw.eq(StringUtils.isNotBlank(bo.getChargeType()), PatVisitIndex::getChargeType, bo.getChargeType());
        lqw.eq(StringUtils.isNotBlank(bo.getExternalCause()), PatVisitIndex::getExternalCause, bo.getExternalCause());
        lqw.eq(StringUtils.isNotBlank(bo.getAutopsyFlag()), PatVisitIndex::getAutopsyFlag, bo.getAutopsyFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getPracticeDoctor()), PatVisitIndex::getPracticeDoctor, bo.getPracticeDoctor());
        lqw.eq(StringUtils.isNotBlank(bo.getEngageDoctor()), PatVisitIndex::getEngageDoctor, bo.getEngageDoctor());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDrugAllerge()), PatVisitIndex::getIsDrugAllerge, bo.getIsDrugAllerge());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugAllergy()), PatVisitIndex::getDrugAllergy, bo.getDrugAllergy());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), PatVisitIndex::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), PatVisitIndex::getRemarks, bo.getRemarks());
        lqw.eq(StringUtils.isNotBlank(bo.getRh()), PatVisitIndex::getRh, bo.getRh());
        lqw.eq(StringUtils.isNotBlank(bo.getAbo()), PatVisitIndex::getAbo, bo.getAbo());
        lqw.eq(StringUtils.isNotBlank(bo.getPathologicalDiagCode()), PatVisitIndex::getPathologicalDiagCode, bo.getPathologicalDiagCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPathologicalNo()), PatVisitIndex::getPathologicalNo, bo.getPathologicalNo());
        lqw.eq(StringUtils.isNotBlank(bo.getPathologicalDiag()), PatVisitIndex::getPathologicalDiag, bo.getPathologicalDiag());
        lqw.eq(StringUtils.isNotBlank(bo.getRegistZipCode()), PatVisitIndex::getRegistZipCode, bo.getRegistZipCode());
        lqw.eq(StringUtils.isNotBlank(bo.getRegistAddress()), PatVisitIndex::getRegistAddress, bo.getRegistAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getRegistAddressCode()), PatVisitIndex::getRegistAddressCode, bo.getRegistAddressCode());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkCode()), PatVisitIndex::getWorkCode, bo.getWorkCode());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkPhone()), PatVisitIndex::getWorkPhone, bo.getWorkPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getExternalCauseCode()), PatVisitIndex::getExternalCauseCode, bo.getExternalCauseCode());
        lqw.eq(bo.getPaymentAdvice() != null, PatVisitIndex::getPaymentAdvice, bo.getPaymentAdvice());
        lqw.eq(StringUtils.isNotBlank(bo.getResidenceNo()), PatVisitIndex::getResidenceNo, bo.getResidenceNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicalPayWay()), PatVisitIndex::getMedicalPayWay, bo.getMedicalPayWay());
        lqw.eq(StringUtils.isNotBlank(bo.getBrainDamageTimeBeforeDay()), PatVisitIndex::getBrainDamageTimeBeforeDay, bo.getBrainDamageTimeBeforeDay());
        lqw.eq(StringUtils.isNotBlank(bo.getBrainDamageTimeBeforeHour()), PatVisitIndex::getBrainDamageTimeBeforeHour, bo.getBrainDamageTimeBeforeHour());
        lqw.eq(StringUtils.isNotBlank(bo.getBrainDamageTimeBeforeMinute()), PatVisitIndex::getBrainDamageTimeBeforeMinute, bo.getBrainDamageTimeBeforeMinute());
        lqw.eq(StringUtils.isNotBlank(bo.getBrainDamageTimeAfterDay()), PatVisitIndex::getBrainDamageTimeAfterDay, bo.getBrainDamageTimeAfterDay());
        lqw.eq(StringUtils.isNotBlank(bo.getBrainDamageTimeAfterHour()), PatVisitIndex::getBrainDamageTimeAfterHour, bo.getBrainDamageTimeAfterHour());
        lqw.eq(StringUtils.isNotBlank(bo.getBrainDamageTimeAfterMinute()), PatVisitIndex::getBrainDamageTimeAfterMinute, bo.getBrainDamageTimeAfterMinute());
        lqw.eq(StringUtils.isNotBlank(bo.getComeAgainIf()), PatVisitIndex::getComeAgainIf, bo.getComeAgainIf());
        lqw.eq(StringUtils.isNotBlank(bo.getComeAgainPurpose()), PatVisitIndex::getComeAgainPurpose, bo.getComeAgainPurpose());
        lqw.eq(StringUtils.isNotBlank(bo.getPersType()), PatVisitIndex::getPersType, bo.getPersType());
        lqw.eq(StringUtils.isNotBlank(bo.getCenterId()), PatVisitIndex::getCenterId, bo.getCenterId());
        lqw.eq(StringUtils.isNotBlank(bo.getEduLevel()), PatVisitIndex::getEduLevel, bo.getEduLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getMatnType()), PatVisitIndex::getMatnType, bo.getMatnType());
        lqw.eq(StringUtils.isNotBlank(bo.getLatechbFlag()), PatVisitIndex::getLatechbFlag, bo.getLatechbFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getPretFlag()), PatVisitIndex::getPretFlag, bo.getPretFlag());
        lqw.eq(bo.getBirctrlMatnDate() != null, PatVisitIndex::getBirctrlMatnDate, bo.getBirctrlMatnDate());
        lqw.eq(StringUtils.isNotBlank(bo.getFpscNo()), PatVisitIndex::getFpscNo, bo.getFpscNo());
        lqw.eq(StringUtils.isNotBlank(bo.getBirctrlType()), PatVisitIndex::getBirctrlType, bo.getBirctrlType());
        lqw.eq(StringUtils.isNotBlank(bo.getGesoVal()), PatVisitIndex::getGesoVal, bo.getGesoVal());
        lqw.eq(StringUtils.isNotBlank(bo.getFetts()), PatVisitIndex::getFetts, bo.getFetts());
        lqw.eq(StringUtils.isNotBlank(bo.getFetusCnt()), PatVisitIndex::getFetusCnt, bo.getFetusCnt());
        lqw.eq(StringUtils.isNotBlank(bo.getLocalFlag()), PatVisitIndex::getLocalFlag, bo.getLocalFlag());
        return lqw;
    }

    /**
     * 新增患者入院基本信息
     */
    @Override
    public Boolean insertByBo(PatVisitIndexBo bo) {
        PatVisitIndex add = BeanUtil.toBean(bo, PatVisitIndex.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setVisitIndexId(add.getVisitIndexId());
            PatVisitVo patVisitVo = patVisitService.insertByVo(bo);
            baseMapper.update(null, Wrappers.<PatVisitIndex>lambdaUpdate()
                    .set(PatVisitIndex::getVisitId, patVisitVo.getVisitId())
                    .eq(PatVisitIndex::getVisitIndexId, add.getVisitIndexId())
            );
            hospitalizationNoticeMapper.update(null,Wrappers.<HospitalizationNotice>lambdaUpdate()
                    .set(HospitalizationNotice::getInFlag, BaseConstants.ENTRY_AND_EXIT_HOSPITAL_STATUS.ONE)
                    .eq(HospitalizationNotice::getNoticeId,bo.getNoticeId())
            );
        }
        return flag;
    }

    /**
     * 修改患者入院基本信息
     */
    @Override
    public Boolean updateByBo(PatVisitIndexBo bo) {
        PatVisitIndex update = BeanUtil.toBean(bo, PatVisitIndex.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PatVisitIndex entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除患者入院基本信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
